﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Interop;

namespace vm_controller
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        //声明默认配置文件路径
        public string INIPath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "config.ini";
        IniFiles ConfigINI = new IniFiles();

        public MainWindow()
        {
            Trace.WriteLine("触发 MainWindow 事件");
            InitializeComponent();
            SetPositionToTopRight();
            string windowName = ConfigINI.INIRead("window", "name", INIPath);
            IntPtr winHanle = FindWindow(null, windowName);
            Trace.WriteLine(windowName);
            Trace.WriteLine("windowName"+ windowName);
            // IntPtr childHandle = FindWindow(null, "VM_Controller"); //GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
            if (!winHanle.Equals(0))
            {
                var mainWindow = Application.Current.MainWindow;
                WindowInteropHelper helper = new WindowInteropHelper(mainWindow);
                IntPtr childHandle = helper.Handle;
                helper.Owner = winHanle;
                // SetParent(childHandle, winHanle);
                // SetWindowLong(childHandle, -8, winHanle);
                _ = ShowWindow(winHanle, 2);
                System.Threading.Thread.Sleep(100);

                //   _ = MoveWindow(childHandle, 1820, 0, 0, 0, true);
                _ = ShowWindow(winHanle, 3);
            }
    
        }
        private void SetPositionToTopRight()
        {
            // 设置窗口的位置
            this.Left = 1860;
            this.Top = 0;
        }
        protected override void OnActivated(EventArgs e)
        {
            //base.OnActivated(e);

           // Debug.WriteLine("触发 OnActivated 事件");

            //IntPtr winHanle = FindWindow(null, "视觉密码.txt - 记事本");

            //  SendMessage(winHanle, WM_SYSCOMMAND2, (int)SC_MINIMIZE, 0);// 最小化

            //_ = ShowWindow(winHanle, 0);
            //  _ = ShowWindow(winHanle, 3);

           // IntPtr childHandle = FindWindow(null, "VM_Controller");
           // _ = ShowWindow(childHandle, 3);
           // this.Show();
        }

        protected override void OnDeactivated(EventArgs e)
        {
            base.OnDeactivated(e);

            Debug.WriteLine("触发 OnDeactivated 事件");
        }



        private void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine("触发OnMouseDown 事件");

            string windowName = ConfigINI.INIRead("window", "name", INIPath);
            IntPtr winHanle = FindWindow(null, windowName);

            _ = ShowWindow(winHanle, 2); 
        }

        private void OnTouchEnter(object sender, TouchEventArgs e)
        {
            Debug.WriteLine("触发OnMouseDown 事件");

            string windowName = ConfigINI.INIRead("window", "name", INIPath);
            IntPtr winHanle = FindWindow(null, windowName);

            _ = ShowWindow(winHanle, 2);
        }



        private void Window_StateChanged(object sender, EventArgs e)
        {
          /*  if (this.WindowState == WindowState.Minimized)
            {
                this.Hide();
            }
            if (this.WindowState == WindowState.Maximized || this.WindowState == WindowState.Normal)
            {
                this.Show();
                this.Activate();
            }*/
        }
            #region API 需要using System.Runtime.InteropServices;

            [DllImport("user32.dll ", EntryPoint = "SetParent")]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);   //将外部窗体嵌入程序

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);      //按照窗体类名或窗体标题查找窗体

        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
        private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);                  //设置窗体属性



        [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr GetModuleHandle(string lpModuleName);


        //hWndChild:要嵌入对象的句柄   hWndParent:嵌入到的容器的句柄
        [DllImport("user32.dll", EntryPoint = "MoveWindow", CharSet = CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);
        //移动窗体到指定的位置
        #endregion



    }
}
